//
//  RefreshHeader.m
//  YunDongGym
//
//  Created by lqq on 2018/7/25.
//  Copyright © 2018年 lqq. All rights reserved.
//


#define kLoadingWidth   50


#import "RefreshHeader.h"
#import "SafeControl.h"
#import "UIColor+Extension.h"


@interface RefreshHeader ()
@property (nonatomic, weak) RefreshHeaderIndicaterView *indicaterView;
@property (nonatomic, weak) UIImageView *animationImageView;
@end

@implementation RefreshHeader

#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置（比如添加子控件）
- (void)prepare
{
    [super prepare];
    
    // 设置控件的高度
    self.mj_h = 50;
    
    RefreshHeaderIndicaterView *indicaterView = [[RefreshHeaderIndicaterView alloc] init];
    indicaterView.backgroundColor = [UIColor clearColor];
    [self addSubview:indicaterView];
    self.indicaterView = indicaterView;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    [self addSubview:imageView];
    NSMutableArray *animationImages = [NSMutableArray array];
    for (int i = 1 ;  i<= 9; i++) {
        NSString *imageName = [NSString stringWithFormat:@"run_%d",i];
        [animationImages safeAddObject:[UIImage imageNamed:imageName]];
    }
    imageView.animationImages = animationImages;
    imageView.animationDuration = 0.5;
    self.animationImageView  = imageView;
    self.animationImageView.hidden = YES;
}

#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    
    self.indicaterView.frame = CGRectMake(0, 0,kLoadingWidth, kLoadingWidth);
    self.indicaterView.center = CGPointMake(self.mj_w * 0.5, self.mj_h * 0.5);
    self.animationImageView.frame = CGRectMake(0, 0, 50, 50);
    self.animationImageView.center = CGPointMake(self.mj_w * 0.5, self.mj_h * 0.5);
    
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
    
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];
    
}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle:
            self.indicaterView.hidden = NO;
            self.indicaterView.progress = 1;
            
            self.animationImageView.hidden = YES;
            break;
        case MJRefreshStatePulling:
            self.indicaterView.hidden = NO;
            self.animationImageView.hidden = YES;
            break;
        case MJRefreshStateRefreshing:
            self.indicaterView.hidden = YES;
            self.animationImageView.hidden = NO;
            [self.animationImageView startAnimating];
            break;
        default:
            break;
    }
    
}

#pragma mark 监听拖拽比例（控件被拖出来的比例）
- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];
    //    NSLog(@"%f",pullingPercent);
    self.indicaterView.progress = pullingPercent;
    
}

@end



@interface RefreshHeaderIndicaterView ()
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation RefreshHeaderIndicaterView


- (instancetype)init
{
    self = [super init];
    if (self) {
        _imageView = [[UIImageView alloc] init];
        [self addSubview:_imageView];
        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        _imageView.image = [UIImage imageNamed:@"run_4"];

    }
    return self;
}

- (void)setProgress:(CGFloat)progress
{
//    if (progress < 0.1) {
//        progress = 0.1;
//    } else if (progress > 0.9) {
//        progress = 0.9;
//    }
//    
//    int count = progress * 100 / 10;
//    _imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"run_%d",count]];
}





@end
